<template>
  <div class="Roles-container">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addGoods"
          >添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="goodsList"
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
        >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格（元）"
        >
        </el-table-column>
        <el-table-column
          prop="goods_number"
          label="商品数量"
        >
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
        >
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <!-- 状态区域 -->
        <el-table-column label='操作'>
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              v-model="scope.row.goods_name"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              v-model="scope.row.goods_name"
              size="mini"
              @click="removeGoodsById(scope.row.goods_id)"
            ></el-button>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 8]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

  </div>

</template>

<script>
export default {
  data() {
    return {
      // 商品列表数据
      goodsList: [],
      // 获取商品列表的参数对象
      queryInfo: {
        query: "",
        // 当前页数
        pagenum: 1,
        // 每页显示条数
        pagesize: 10
      },
      // 总数与条数
      total: 0,
      // 控制添加商品对话框的显示与隐藏
      addDialogVisible: false,
      // 添加商品列表的表单数据
      addGoodsFrom: {},
      // 添加商品列表的验证规则对象
      addGoodsFromRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", tirgger: "blur" }
        ],
        goods_cat: [
          { required: true, message: "请输入分割的分类", tirgger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入价格", tirgger: "blur" }
        ],
        goods_number: [
          { required: true, message: "请输入数量", tirgger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入重量", tirgger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$Message.error("获取用户列表失败")
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(this.goodsList)
    },
    // 监听pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 点击添加商品按钮跳转到add组件
    addGoods() {
      this.$router.push("/goods/add")
    },
    async removeGoodsById(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err)
      // 如果用户确认删除则返回值为字符串 confirm
      // 如果用户取消删除这返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== "confirm") {
        return this.$Message.info("已经取消删除")
      }
      console.log("确认删除")
      const { data: res } = await this.$http.delete("goods/" + id)
      if (res.meta.status !== 200) {
        return this.$Message.error("删除失败")
      }
      this.getGoodsList()
      this.$Message.success("删除成功")
    }
  }
}
</script>

<style lang="less" scoped>
</style>
